<?php defined('BASEPATH') OR exit('No direct script access allowed');


class Language_model extends WS_model
{
    public function getAll_languages()
    {
        $this->db->from('languages');
        $this->db->order_by('language_name', 'asc');
        $query = $this->db->get();
        if ($query->result())
            return $query->result();
        return false;
    }

    public function get_defaultLanguages($filter = true)
    {
        $this->db->from('language_grouping lg');
        $this->db->join('languages l', 'l.language_seq_id = lg.language_seq_id', 'left');
        $query = $this->db->get();
        if ($query->result()) {
            if ($filter) {
                return $this->get_defaultLanguages_filter($query->result());
            } else {
                return $query->result();
            }
        }
        return false;
    }

    public function get_languageGroupingByID($id)
    {
        $this->db->from('language_grouping lg');
        $this->db->join('languages l', 'l.language_seq_id = lg.language_seq_id', 'left');
        $this->db->where('lg.lg_seq_id', $id);
        $query = $this->db->get();
        if ($query->result()) {
            return $query->row();
        }
        return false;
    }

    private function  get_defaultLanguages_filter($data)
    {
        $return = array();
        if (!$data)
            return false;
        foreach ($data as $d) {
            $return[$d->lg_name] = $d->language_seq_id;
        }
        return $return;
    }

    public function get_languageById($id)
    {
        $this->db->from('languages');
        $this->db->where('language_seq_id', $id);
        $query = $this->db->get();
        if ($query->result())
            return $query->row();
        return false;
    }

    public function activate_grouping_language($group_id, $language_id)
    {
        $this->db->update('language_grouping', array('language_seq_id' => $language_id), array('lg_seq_id' => $group_id));
        return $this->db->affected_rows();
    }

    public function get_Default_language($group)
    {
        $this->db->from('language_grouping lg');
        $this->db->join('languages l', 'l.language_seq_id = lg.language_seq_id', 'left');
        $this->db->where('lg_name', $group);
        $query = $this->db->get();
        if ($query->result())
            return $query->row();
        return false;
    }
}